<!--新增编辑公司 -->
<template>
  <div class="detail-index-content">
    <Modal
      v-model="modalValue"
      :title="titleName"
      footer-hide
      width="600px"
      @on-cancel="cancel"
    >
      <Form ref="formData" :rules="ruleValidate" :model="formData">
        <Row :gutter="30">
          <Col span="24">
            <FormItem label="公司名称" prop="companyName">
              <div style="width: 100%">
                <Input
                  v-model="formData.companyName"
                  placeholder="请输入"
                ></Input>
              </div>
            </FormItem>
          </Col>
        </Row>
        <Row type="flex" justify="space-between">
          <Col>
            <Button
              type="primary"
              :loading="isLoading"
              @click="handleSubmit('formData')"
              >保存</Button
            >
          </Col>
        </Row>
      </Form>
    </Modal>
  </div>
</template>

<script type="text/javascript">
import {
  addCompany, // 创建公司
  updateCompany, // 修改公司
} from "@/api/system.js";

export default {
  name: "addUpdateCompany",
  components: {},
  props: {
    echoData: {
      type: Object,
      default: () => {},
    },
    isHide: {
      type: Boolean,
      default: false,
    },
    titleName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formData: {
        parentId: "",
        companyName: "",
      },
      ruleValidate: {
        companyName: [
          {
            required: true,
            message: "请输入公司名称",
            trigger: "change",
          },
        ],
      },
      modalValue: false,
      isLoading: false,
    };
  },
  methods: {
    cancel() {
      this.$emit("update:updateIsHide", false);
    },
    async addCompanyAction() {
      try {
        console.log(this.$props.echoData, "this.$echoData");
        this.formData.parentId = this.$props.echoData.id;
        console.log(2);
        this.isLoading = true;
        let data = await addCompany(this.formData);
        this.isLoading = false;
        this.$Message.success("添加成功！");
        this.$emit("actionSuccess");
        this.$emit("update:updateIsHide",false)
      } catch (error) {
        this.isLoading = false;
      }
    },
    async updateCompanyAction() {
      this.isLoading = true;
      try {
        let data = await updateCompany(this.formData);
        this.isLoading = false;
        this.$Message.success("编辑成功！");
        this.$emit("update:updateIsHide",false)
        this.$emit("actionSuccess");
      } catch (error) {
        this.isLoading = false;
      }
    },
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        console.log(valid, "name", this.$props.echoData);
        if (valid) {
          if (this.$props.titleName == "编辑") {
            this.formData.parentId = this.$props.echoData.id;
            this.formData.companyName = this.$props.echoData.title;
            return this.updateCompanyAction();
          }
          this.addCompanyAction();
        }
      });
    },
  },
  created() {
    this.formData.parentId = this.$props.echoData.parentId;
    this.formData.ownType = this.$props.echoData.ownType;
    this.modalValue = this.$props.isHide;
  },
};
</script>

<style lang="less" scoped>
.ivu-radio-group {
  width: 100%;
}
</style>
